package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.qf.Order;
import com.qf.ResultVo;
import com.qf.Seat;
import com.qf.dto.OrderDTO;
import com.qf.mapper.OrderMapper;
import com.qf.mapper.SeatMapper;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 作者：Xx凯丶
 * 时间：2022/4/25 9:14
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SeatMapper seatMapper;
    @Override
    public ResultVo saveOrder(OrderDTO orderDTO) {
        Integer[] seats = orderDTO.getSeats();
        Order order = new Order();
        order.setUserId(orderDTO.getUserId());
        order.setMovieName(orderDTO.getMovieName());
        order.setMovieImg(orderDTO.getMovieImg());
        order.setCinemaName(orderDTO.getCinemaName());
        order.setHallName(orderDTO.getHallName());
        order.setOrderTime(orderDTO.getOrderTime());
        order.setOrderPrice(orderDTO.getOrderPrice());
        String seatInfo="";


        for (Integer seatId : seats) {
            Seat seat = seatMapper.selectSeatById(seatId);
            seatMapper.updateStatusById(seatId);
            String info=seat.getRow()+"排"+seat.getColumn()+"座"+"  ";
            seatInfo+=info;
        }
        order.setSeats(seatInfo);
        int i = orderMapper.insert(order);

        return new ResultVo(200,"SUCCESS",order);
    }

    @Override
    public boolean changeOrder(String orderId) {
        LambdaUpdateChainWrapper<Order> wrapper = new LambdaUpdateChainWrapper<>(orderMapper);
        boolean update = wrapper.set(Order::getOrderStatus, 1).eq(Order::getOrderId, orderId).update();
        return update;
    }
}
